package org.xjc.utils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public final class JsonUtil {
    public static boolean failOnUnknownProperties = false;
    public static boolean failOnNullForPrimitives = false;
    public static boolean ignoreNullProperties = false;
    public static boolean failOnEmptyBeans = false;

    public JsonUtil() {
    }

    public static <T> T parseObject(String text, Class<T> clazz) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties);
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, failOnNullForPrimitives);
        return parseObject(objectMapper, text, clazz);
    }

    public static Map<String, Object> parseObject(String text) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties);
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, failOnNullForPrimitives);
        return parseObject(objectMapper, text);
    }

    public static <T> List<T> parseArray(String text, Class<T> clazz) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties);
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, failOnNullForPrimitives);
        return parseArray(objectMapper, text, clazz);
    }

    public static List<Map<String, Object>> parseArray(String text) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties);
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, failOnNullForPrimitives);
        return parseArray(objectMapper, text);
    }

    public static Object parse(String text) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties);
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, failOnNullForPrimitives);
        return parse(objectMapper, text);
    }

    public static <T> T parse(String text, TypeReference<T> typeReference) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties);
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, failOnNullForPrimitives);
        return parse(objectMapper, text, typeReference);
    }

    public static JsonNode toJsonNode(String text) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties);
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, failOnNullForPrimitives);
        return toJsonNode(objectMapper, text);
    }

    public static String toJSONString(Object object) {
        ObjectMapper objectMapper = new ObjectMapper();
        if (ignoreNullProperties) {
            objectMapper.setSerializationInclusion(Include.NON_NULL);
        }

        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, failOnEmptyBeans);
        return toJSONString(objectMapper, object);
    }

    public static <T> T parseObject(ObjectMapper objectMapper, String text, Class<T> clazz) {
        if (!StringUtils.isEmpty(text)) {
            try {
                return objectMapper.readValue(text, clazz);
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

        return null;
    }

    public static Map<String, Object> parseObject(ObjectMapper objectMapper, String text) {
        if (!StringUtils.isEmpty(text)) {
            try {
                return (Map) objectMapper.readValue(text, new TypeReference<Map<String, Object>>() {
                });
            } catch (Exception var3) {
                var3.printStackTrace();
            }
        }

        return null;
    }

    public static <T> List<T> parseArray(ObjectMapper objectMapper, String text, Class<T> clazz) {
        if (!StringUtils.isEmpty(text)) {
            try {
                return (List) objectMapper.readValue(text, objectMapper.getTypeFactory().constructParametricType(List.class, new Class[]{clazz}));
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

        return null;
    }

    public static List<Map<String, Object>> parseArray(ObjectMapper objectMapper, String text) {
        if (!StringUtils.isEmpty(text)) {
            try {
                return (List) objectMapper.readValue(text, new TypeReference<List<Map<String, Object>>>() {
                });
            } catch (Exception var3) {
                var3.printStackTrace();
            }
        }

        return null;
    }

    public static Object parse(ObjectMapper objectMapper, String text) {
        if (!StringUtils.isEmpty(text)) {
            try {
                JsonNode node = objectMapper.readTree(text);
                if (node.isObject()) {
                    return objectMapper.readValue(node.traverse(), new TypeReference<Map<String, Object>>() {
                    });
                }

                return objectMapper.readValue(node.traverse(), new TypeReference<List<Map<String, Object>>>() {
                });
            } catch (Exception var3) {
                var3.printStackTrace();
            }
        }

        return null;
    }

    public static <T> T parse(ObjectMapper objectMapper, String text, TypeReference<T> typeReference) {
        if (!StringUtils.isEmpty(text)) {
            try {
                return objectMapper.readValue(text, typeReference);
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

        return null;
    }

    public static JsonNode toJsonNode(ObjectMapper objectMapper, String text) {
        if (!StringUtils.isEmpty(text)) {
            try {
                return objectMapper.readTree(text);
            } catch (Exception var3) {
                var3.printStackTrace();
            }
        }

        return null;
    }

    public static String toJSONString(ObjectMapper objectMapper, Object object) {
        if (object != null) {
            try {
                return objectMapper.writeValueAsString(object);
            } catch (Exception var3) {
                var3.printStackTrace();
            }
        }

        return null;
    }
}